<!DOCTYPE html>
<html xmlns:th="http://www.w3.org/1999/xhtml">

<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>商品分类管理</title>
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <link rel="stylesheet" href="/plugins/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="/plugins/adminLTE/css/AdminLTE.css">
    <link rel="stylesheet" href="/plugins/adminLTE/css/skins/_all-skins.min.css">
    <link rel="stylesheet" href="/css/style.css">
	<script src="/plugins/jQuery/jquery-2.2.3.min.js"></script>
    <script src="/plugins/bootstrap/js/bootstrap.min.js"></script>
	<!--bootstrap-toastr-->
	<script type="text/javascript" src="/bootstrap/toastr/toastr.min.js"></script>
	<link rel="stylesheet" type="text/css" href="/bootstrap/toastr/toastr.css">

	<script>
		$(function () {
			getListItemCat();
		})

		function getListItemCat() {
			getItemName(parent_Id);
			$.ajax({
				url : "/itemCat/getListItemCat",
				dataType : "json",
				type : "post",
				data : {pageNumber : $("#pageNumber").val(),parentId : parent_Id},
				success : function (returnData) {

					var result = returnData.data.list;
					var table = '';
					for (var i = 0; i < result.length; i++) {
						table += '<tr>'
						table += '<td><input name="box" value="'+ result[i].id +'" type="checkbox" ></td>'
						table += '<td>'+ result[i].id +'</td>'
						table += '<td>'+ result[i].name +'</td>'
						table += '<td>'+ result[i].typeId +'</td>'
						table += '<td class="text-center"> '
						table += '<button type="button" class="btn bg-olive btn-xs" onclick="getNextItem('+result[i].id+')" >查询下级</button>'
						table += '<button type="button" class="btn bg-olive btn-xs" data-toggle="modal" data-target="#updateItemCatModal" onclick="toUpdateItemCat('+result[i].id+')">修改</button> '
						table += "</td>"
						table += "</tr>"
					}

					$("#itemCatList").html(table);

					$("#showCount").html(returnData.data.total)
					$("#showPage").html(returnData.data.pages)

					$("#totalPage").val(returnData.data.pages)
					$("#totalCount").val(returnData.data.total)
					$("#pageNumber").val(returnData.data.pageNumer)

					var rowPages = ""
					for (var j = 1;j <= returnData.data.pages;j++){
						rowPages += "<li>"
						rowPages += "<a href='javascript:changePageNumber("+ j +")'>"+ j +"</a>"
						rowPages += "</li>"
						$("#pages").html(rowPages)
					}
				},
				error : function () {

				}
			})
		}
		
		function getItemName(parent_Id) {
			if (parent_Id == null){
				return;
			}
			$.ajax({
				url:"/itemCat/getItemName",
				type:"post",
				data:{id : parent_Id},
				dataType:"json",
				success:function (returnData) {
					if (returnData.code == 200){
						var itemCat = returnData.data;
						var li = '<li id="'+ itemCat.id +'">' +
								'<a href="javascript:getNextItem('+itemCat.id+')" >'+itemCat.name+'</a></li>';
						$("#classify").append(li);
						$("#"+itemCat.id).nextAll().remove();

						var li = $("#classify").find("li").find("a");
						var text = "";
						$.each(li,function (i,e) {
							if(i == li.length - 1){
								text += e.text;
							}else{
								text += e.text+">>";
							}
						});
						$("#itemNameInsert").html(text);
						$("#itemNameUpdate").html(text);
					}else {
						toastr.error(returnData.message);
					}

				},
				error:function () {
					toastr.error("系统错误")
				}
			})
		}

		var parent_Id = null;
		function getNextItem(parentId) {
			parent_Id = parentId;
			getListItemCat(parentId);
		}


		function getPage(flag) {
			var pageNumber = parseInt($("#pageNumber").val());
			var totalPage = parseInt($("#totalPage").val());

			if (flag == 1){
				if (pageNumber <= 1){
					toastr.warning("已经是第一页");
					return;
				}
				$("#pageNumber").val(1);
			}else if (flag == 2) {
				if (pageNumber <= 1){
					toastr.warning("已经是第一页");
					return;
				}
				$("#pageNumber").val(pageNumber - 1);
			}else if (flag == 3){
				if (pageNumber >= totalPage){
					toastr.warning("已经是最后一页");
					return;
				}
				$("#pageNumber").val(pageNumber + 1);
			} else if (flag == 4) {
				if (pageNumber >= totalPage){
					toastr.warning("已经是最后一页");
					return;
				}
				$("#pageNumber").val(totalPage);
			}
			getNextItem(parent_Id);
		}

		function changePageNumber(obj) {
			$("#pageNumber").val(obj);
			getNextItem(parent_Id);
		}

		function reload() {
			window.location.reload();
		}

		function insertItemCat() {
			if (parent_Id == null){
				var parentId = 0;
			}else {
				var parentId = parent_Id;
			}

			$.ajax({
				url : "/itemCat/insertItemCat",
				type : "post",
				data : {name : $("#insertItemName").val(), typeId : $("#insertTemplateId").val(), parentId : parentId},
				dataType : "json",
				success : function (returnData) {
					if (returnData.code == 200) {
						toastr.success(returnData.message);
					} else {
						toastr.error(returnData.message);
					}
					setTimeout(function(){
								reload();
							},1000
					);
				},
				error : function (returnData) {
					toastr.error("系统错误");
				}
			})
		}
		
		function toUpdateItemCat(id) {
			$.ajax({
				url : "/itemCat/toUpdateItemCat",
				type : "post",
				data : {id : id},
				dataType : "json",
				success : function (returnData) {
					if (returnData.code == 200) {
						var itemCar = returnData.data;
						$("#updateItemName").val(itemCar.name);
						$("#updateItemCatId").val(itemCar.id);
						$("#updateParentId").val(itemCar.parentId);
						$("#updateTemplateId").val(itemCar.typeId);

					} else {
						toastr.error(returnData.message);
					}
				},
				error : function (returnData) {
					toastr.error("系统错误");
				}
			})
		}
		
		function updateItemCat() {
			$.ajax({
				url : "/itemCat/updateItemCat",
				type : "post",
				data : {id : $("#updateItemCatId").val(), parentId : $("#updateParentId").val(), name: $("#updateItemName").val(), typeId : $("#updateTemplateId").val()},
				dataType : "json",
				success : function (returnData) {
					if (returnData.code == 200) {
						toastr.success(returnData.message);
					} else {
						toastr.error(returnData.message);
					}
					setTimeout(function(){
								reload();
							},1000
					);
				},
				error : function () {
					toastr.error("系统错误");
				}
			})
		}

		//删除
		function deleteBatch() {
			if (confirm("确定删除吗？")) {
				var names = $("[name=box]:checked");
				var ids = [];
				$(names).each(function (i, e) {
					ids.push(e.value)
				});
				if (ids <= 0) {
					toastr.warning("请选择要删除的内容")
					return;
				}
				$.ajax({
					url : "/itemCat/deleteBatch",
					dataType : "json",
					type : "post",
					data : {ids : ids},
					success : function (resultData) {
						if (resultData.code == 200) {
							toastr.success(resultData.message);
						}else {
							toastr.error(resultData.message)
						}
						setTimeout(function(){
									reload();
								},1000
						);
					},
					error : function () {
						toastr.error("系统错误")
					}
				})
			}
		}
	</script>
    
</head>

<body class="hold-transition skin-red sidebar-mini" >
  <!-- .box-body -->
                
                    <div class="box-header with-border">
                        <h3 class="box-title">商品分类管理     
                       	</h3>
                    </div>

                    <div class="box-body">
                  			 <ol class="breadcrumb" id="classify">
                        		<li id="Top">
		                        	<a href="javascript:reload()" >顶级分类列表</a>
		                        </li>
	                        </ol>

                        <!-- 数据表格 -->
                        <div class="table-box">
							
                            <!--工具栏-->
                            <div class="pull-left">
                                <div class="form-group form-inline">
                                    <div class="btn-group">
                                        <button type="button" class="btn btn-default" title="新建" data-toggle="modal" data-target="#insertItemCatModal" ><i class="fa fa-file-o"></i> 新建</button>
                                        <button type="button" class="btn btn-default" title="删除" onclick="deleteBatch()"><i class="fa fa-trash-o"></i> 删除</button>
                                        <button type="button" class="btn btn-default" title="刷新" onclick="reload()"><i class="fa fa-check"></i> 刷新</button>
                                       
                                    </div>
                                </div>
                            </div>                          
                       		
                        
			                <!--数据列表-->
			                  <table id="dataList" class="table table-bordered table-striped table-hover dataTable">
			                      <thead>
			                          <tr>
			                              <th class="" style="padding-right:0px">
			                                  <input type="checkbox" class="icheckbox_square-blue">
			                              </th> 
										  <th class="sorting_asc">分类ID</th>
									      <th class="sorting">分类名称</th>									   
									      <th class="sorting">类型模板ID</th>
									     						
					                      <th class="text-center">操作</th>
			                          </tr>
			                      </thead>
			                      <tbody id="itemCatList">

			                      </tbody>
			                  </table>
			                  <!--数据列表/-->
							<div align="center">
								<nav aria-label="Page navigation">
									<input type="hidden" id="pageNumber" name="pageNumber">
									<input type="hidden" id="totalPage">
									<div class="page_num_inf">
										<i></i> 共
										<span id="showPage"></span>页<span id="showCount"></span>条
									</div>
									<ul class="pagination">
										<li>
											<a href="javascript:getPage(1);" aria-label="Previous">
												<span aria-hidden="true">首页</span>
											</a>

											<a href="javascript:getPage(2);" aria-label="Previous">
												<span aria-hidden="true">&laquo;上一页</span>
											</a>
										</li>
									</ul>
									<ul class="pagination" id="pages">

									</ul>
									<ul class="pagination">
										<li>
											<a href="javascript:getPage(3);" aria-label="Next">
												<span aria-hidden="true">下一页&raquo;</span>
											</a>
											<a href="javascript:getPage(4);" aria-label="Previous">
												<span aria-hidden="true">尾页</span>
											</a>
										</li>
									</ul>
								</nav>
							</div>
                        </div>
                        <!-- 数据表格 /-->
                     </div>
                    <!-- /.box-body -->
              
                                
<!-- 增加窗口 -->
<div class="modal fade" id="insertItemCatModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog" >
	<div class="modal-content">
		<div class="modal-header">
			<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
			<h3 id="myInsertModalLabel">商品分类增加</h3>
		</div>
		<div class="modal-body">							
			
			<table class="table table-bordered table-striped"  width="800px">
				<tr>
		      		<td>上级商品分类</td>
		      		<td id="itemNameInsert">
		      		</td>
		      	</tr>
		      	<tr>
		      		<td>商品分类名称</td>
		      		<td><input name="itemName" id="insertItemName" class="form-control" placeholder="商品分类名称">  </td>
		      	</tr>			  
		      	<tr>
		      		<td>类型模板</td>
		      		<td>
						<select id="insertTemplateId" name="insertTemplates" class="form-control">
							<option th:each="templates : ${templates}" th:value="${templates.id}">[[${templates.name}]]</option>
						</select>
		      		</td>		      		      		
		      	</tr>		      	
			 </table>				
			
		</div>
		<div class="modal-footer">						
			<button class="btn btn-success" data-dismiss="modal" aria-hidden="true" onclick="insertItemCat()">保存</button>
			<button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
		</div>
	  </div>
	</div>
</div>
  <!-- 修改窗口 -->
  <div class="modal fade" id="updateItemCatModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
	  <div class="modal-dialog" >
		  <div class="modal-content">
			  <div class="modal-header">
				  <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
				  <h3 id="myUpdateModalLabel">商品分类修改</h3>
			  </div>
			  <div class="modal-body">

				  <table class="table table-bordered table-striped"  width="800px">
					  <tr>
						  <td>上级商品分类</td>
						  <td id="itemNameUpdate">
						  </td>
					  </tr>
					  <tr>
						  <input type="hidden" name="updateItemCatId" id="updateItemCatId">
						  <input type="hidden" name="updateParentId" id="updateParentId">
						  <td>商品分类名称</td>
						  <td><input name="itemName" id="updateItemName" class="form-control" placeholder="商品分类名称">  </td>
					  </tr>
					  <tr>
						  <td>类型模板</td>
						  <td>
							  <select id="updateTemplateId" name="insertTemplates" class="form-control">
								  <option th:each="templates : ${templates}" th:value="${templates.id}">[[${templates.name}]]</option>
							  </select>
						  </td>
					  </tr>
				  </table>

			  </div>
			  <div class="modal-footer">
				  <button class="btn btn-success" data-dismiss="modal" aria-hidden="true" onclick="updateItemCat()">保存</button>
				  <button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
			  </div>
		  </div>
	  </div>
  </div>
</body>
</html>